package com.mvc.service.healthmanager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.huangqj.classification.Sugar;
import org.huangqj.consant.StringUtils;
import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.dao.BaseDaoI;
import com.mvc.dao.mapper.BloodSugarDao;
import com.mvc.framework.constant.GlobalConstant;
import com.mvc.model.healthmanager.archive.BloodPressure;
import com.mvc.model.healthmanager.archive.BloodSugar;
import com.mvc.service.healthmanager.BloodSugarServiceI;
import com.mvc.utils.CreatHighChartJson;
import com.mvc.utils.StringUtil;
/**
 * @author hejie
 *
 */
@Service
public class BloodSugarServiceImpl implements BloodSugarServiceI {
	private final String titleName = "血糖数据走势";
	private final String unit = "mmol/L";
	private final String yAxixname = "血糖(mmol/L)";
	@Autowired
	private BloodSugarDao bsDao;
	@Autowired
	private BaseDaoI<BloodSugar> sDaoI;
	@Autowired
	private BaseDaoI<BloodPressure>pDaoI;
	/**
	 * 根据条件查询血糖数据
	 * @param bSugar
	 * @return
	 */
	@Override
	public List<BloodSugar> getBloodSugar(BloodSugar bSugar) {
		return bsDao.getBloodSugar(bSugar);
	}

	/**
	 * 获取最新血糖记录
	 * @author qingjie
	 * @param personId
	 */
	@Override
	public BloodSugar getNewBloodSugar(BloodSugar bs) {
		Map<String, Object> params = StringUtils.beanToMap(bs);
		List<BloodSugar> list = sDaoI.find(StringUtil.getHql("BloodSugar", params, "desc"), params);
		return list.size()>0?list.get(0):new BloodSugar();
	}

	/**
	 * @author qingjie
	 * 根据条件血糖数据，生成json
	 * Date:2015-11-15
	 */
	@Override
	public String getJsonData(BloodSugar bSugar) {
		List<BloodSugar> list = bsDao.getBloodSugar(bSugar);
		String[] xAxis = new String[list.size()];
		Map<String, Object> map = new HashMap<String, Object>(); 
		if(!list.isEmpty()){
			Map<String, Object> fbg_m = new HashMap<String, Object>(); 
			fbg_m.put("name", "餐前血糖(mmol/L)");
			List<Double> fbg_l = new ArrayList<Double>();
			Map<String, Object> ogtt_m = new HashMap<String, Object>(); 
			ogtt_m.put("name", "餐后血糖(mmol/L)");
			List<Double> ogtt_l = new ArrayList<Double>();
			Map<String, Object> glu_m = new HashMap<String, Object>(); 
			glu_m.put("name", "随机血糖(mmol/L)");
			List<Double> glu_l = new ArrayList<Double>();
			
			for(int i=0; i<list.size(); i++){
				BloodSugar sugar = list.get(i);
				xAxis[i] = GlobalConstant.SDF.format(sugar.getCreatDate());
				fbg_l.add(i, sugar.getFbg());
				ogtt_l.add(i, sugar.getOgtt());
				glu_l.add(i, sugar.getGlu());
			}
			fbg_m.put("data", fbg_l);ogtt_m.put("data", ogtt_l);glu_m.put("data", glu_l);
			map.put("fbg", fbg_m);map.put("ogtt", ogtt_m);map.put("glu", glu_m);
		}
		return CreatHighChartJson.CreatJsonData(titleName, unit, yAxixname, xAxis, map);
	}

	@Override
	public List<BloodSugar> findNewBloodPressure(Map<String, Object> params,
			String order) {
		return sDaoI.find(StringUtil.getHql("BloodSugar", params, order),params);
	}

	/**
	 * @author Roy
	 * 根据不同条件查询血糖表
	 * @param param
	 * @return
	 */
	@Override
	public List<BloodSugar> findBloodSugars(Map<String, Object> params,String order) {
		return sDaoI.find(StringUtil.getHql2("BloodSugar", params)+" order by t.count "+order, params);
	}

	
	@Override
	public BloodSugar saveOrUpdate(BloodSugar bs) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("personId", bs.getPersonId());
		List<BloodSugar> list = sDaoI.find(StringUtil.getHql("BloodSugar", params, "desc"),params);
		if(list.size()>0){
			//设置服务次数
			int count = ValidatorUtil.isNullOnIntger(list.get(0).getCount())?0:list.get(0).getCount();
			bs.setCount(count+1);
		}
		bs.setModifyDate(bs.getCreatDate());
		bs.setDataSource("手机app");
		try {
			//设置下次服务时间
			bs.setNext_serviceDate(GlobalConstant.SDF2.parse(StringUtil.getNextTime(GlobalConstant.SDF2.format(bs.getCreatDate()), 3)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		double ogtt = 0.0;
		if(!ValidatorUtil.isNullOnDouble(bs.getOgtt())){
			//设置血糖状态
			ogtt = bs.getOgtt();
		}
		if(!ValidatorUtil.isNullOnDouble(bs.getGlu())){
			ogtt = bs.getGlu();
		}
		bs.setSugarState(Sugar.sugarState(ValidatorUtil.isNullOnDouble(bs.getFbg())?0.0:bs.getFbg(), ogtt)+"");
		sDaoI.saveOrUpdate(bs);
		return bs;
	}
	
}
